<template>
  <div class="app-container task">
    <div class="task-header">
      <div class="search-box">
        <el-input
          v-model="searchText"
          placeholder="请输入搜索内容"
          clearable
          @keyup.enter.native="doSearch"
          @clear="_fetchTaskData(searchText)" />
      </div>
    </div>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        label="任务">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.jobName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="用户">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.username ? scope.row.username : scope.row.userId }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="开始时间">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.createdTime * 1000).Format("yyyy-MM-dd hh:mm:ss") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="size"
        label="状态">
        <template slot-scope="scope">
          <span :class="[scope.row.statusClass]" class="status-box">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="结束时间">
        <template slot-scope="scope">
          <span>{{ scope.row.completedTime ? new Date(scope.row.completedTime * 1000).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="size"
        label="用时">
        <template slot-scope="scope">
          <span>{{ scope.row.duration }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="size"
        label="操作">
        <template slot-scope="scope">
          <a class="table-action" @click="toDetail(scope.row)">查看</a>
          <a v-if="scope.row.status === '运行中' || scope.row.status === '等待'" class="table-action" @click="stopTask(scope.row)">停止</a>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getTaskList, taskOperationStop } from '@/api/task'
import { computeTaskStatus, computedDuration } from '@/utils'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      clusterOptions: [],
      searchText: ''
    }
  },
  created() {
    this._fetchTaskData()
  },
  methods: {
    // 点击table名称
    toDetail(row) {
      const taskInfo = {
        taskName: row.jobName,
        taskStatus: row.status,
        completedTime: row.completedTime,
        createdTime: row.createdTime,
        duration: row.duration
      }
      this.$store.dispatch('SaveCurTaskInfo', taskInfo)
      this.$router.push({ path: '/task/detail/result' })
    },
    // 停止任务
    stopTask(row) {
      this.$confirm('此操作将停止该任务, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._taskOperation(row.name)
      })
    },
    // 搜索
    doSearch() {
      this._fetchTaskData(this.searchText)
    },
    // 查询任务列表
    _fetchTaskData(searchText = '') {
      this.listLoading = true
      const param = searchText !== '' ? `?search_multi_key=${searchText}` : ''
      getTaskList(param).then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.list = res.data
          this.list.map(item => {
            const createdTime = item.createdTime ? item.createdTime : item.createTime
            const computedStatus = computeTaskStatus({ state: item.status, executionType: item.executionType })
            item.duration = computedDuration(createdTime, item.completedTime)
            item.createdTime = createdTime
            item.status = computedStatus.val
            item.statusClass = computedStatus.className
          })
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 任务停止操作
    _taskOperation(jobName) {
      const para = `/${jobName}`
      const data = {
        executionType: 'STOP'
      }
      taskOperationStop(data, para).then(res => {
        if (res.message.code === 0) {
          this._fetchTaskData('')
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    computeTaskStatus(status) {
      console.log(status)
      return computeTaskStatus({ state: status });
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.task {
  &-header {
    padding: 10px;
    background: #ffffff;
    font-size: 14px;
    color: #303133;
    font-weight: bold;
    height: 48px;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
    .search-box {
      float: right;
    }
    .el-input__inner {
      height: 28px;
    }
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    &:hover {
      color: #4285f4;
    }
  }
  .status-box {
    border-radius: 5px;
    padding: 0 5px;
    color: #000000;
  }
  .status-success {
    background: #63D4AD;
  }
  .status-run {
    background: #85D8F7;
  }
  .status-wait {
    background: #f39c12;
  }
  .status-stop {
    background: #E0E3E8;
  }
  .status-failure {
    background: #F87D8D
  }
}
</style>
